package com.example.demo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.PageUtils;
import com.example.demo.common.StringUtils;
import com.example.demo.constants.pojo.PermissionInfo;
import com.example.demo.constants.pojo.RoleInfo;
import com.example.demo.constants.pojo.UserInfo;
import com.example.demo.constants.pojo.UserRoleResultMap;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.IUserMapper;
import com.example.demo.sao.ICacheSao;
import com.example.demo.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {

	@Autowired
	IUserMapper userMapper;
	
	public UserInfo QueryUserInfoByUserName(String userName) {
    	 
    	 UserInfo user = null;
    	 
    	 if(!StringUtils.IsNullOrEmpty(userName)) {

    		 user = new UserInfo();
    		 
    		 user.setUserId("111111");
        	 user.setName("李四");
        	 user.setUserName(userName);
        	 user.setPassword("1234");
        	
        	 List<RoleInfo> roles=new  ArrayList<RoleInfo>();
        	 StringBuffer sb=new  StringBuffer();
        	 for (int i = 0; i < 2; i++) {
        		 RoleInfo role=new RoleInfo();
            	 role.setRoleId("11010"+i);
            	 String roleName="";
            	 if(i==0) {
            		 roleName="研发部主任";        		
            	 }        	 
            	 else {
            		 roleName="片区经理";        		 
    			 }
            	 role.setRoleName(roleName);
            	 role.setDepartmentId("23434");
            	 
            	 List<PermissionInfo> rolePermissions=new ArrayList<PermissionInfo>();
            	 for (int j = 0; j < 1; j++) {
            		 PermissionInfo permission=new PermissionInfo();
            		 permission.setId(""+j+"");
            		 permission.setPermission("报表查看");
            		 rolePermissions.add(permission);
            	 }
            	 role.setPermissions(rolePermissions);
            	 
            	 roles.add(role);
            	 sb.append(roleName);
        	 }
        	 
        	 user.setRoles(roles);
        	 user.setRolesDescription(sb.toString());
    		 
    	 }
    	 
    	 return user;
	}
	
	
	public IPage<Map<String, Object>> QueryUserList(Map<String, Object> params){
		
		QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
		for (Map.Entry<String, Object> entry : params.entrySet()) {	
			
			if(!entry.getKey().equals("pageSize") &&
					!entry.getKey().equals("currentPage") ) {
				wrapper.eq(entry.getKey(), entry.getValue());
			}	
			
		}
		wrapper.orderByDesc("id");
		
		int pageSize=(int)params.get("pageSize");
		int currentPage=(int)params.get("currentPage");		
		IPage<UserEntity> pager=new Page<>(currentPage,pageSize);		 
		 
		IPage<Map<String, Object>> result = userMapper.selectMapsPage(pager, wrapper);
		
		System.out.println("总页数"+result.getPages());
	    System.out.println("总记录数"+result.getTotal()); 

		return result;
		
	}
	
	
	public IPage<UserEntity> QueryUserList(int current,int size,QueryWrapper<UserEntity> wrapper){
		
		IPage<UserEntity> pager=new Page<>(current,size);	
		
		IPage<UserEntity> result = userMapper.selectPage(pager, wrapper);
		
		System.out.println("总页数"+result.getPages());
	    System.out.println("总记录数"+result.getTotal());

		return result;
		
	}
	
	public List<Map<String, Object>> QueryUserList(Page<Map<String,Object>> page,Map<String, Object> params){

		return userMapper.GetUserList(page,params);		
	}	
	 
	@SuppressWarnings("unchecked")
	public IPage<UserRoleResultMap> QueryUserRoleList(Map<String,Object> params){
		
		int pageSize=(int)params.get("pageSize");
		int currentPage=(int)params.get("currentPage");		
		Page<UserRoleResultMap> result=new Page<>(currentPage,pageSize);

		List<UserRoleResultMap> records=userMapper.SelectUserRoleList(result, params);
		result.setRecords(records);		
 
		return result;
		
	}
	
	@SuppressWarnings("unchecked")
	public IPage<Map<String, Object>> QueryUserRoleListMap(Map<String,Object> params){
		
		int pageSize=(int)params.get("pageSize");
		int currentPage=(int)params.get("currentPage");		
		Page<Map<String, Object>> result=new Page<>(currentPage,pageSize);

		List<Map<String, Object>> records=userMapper.QueryUserRoleList(result, params);
		result.setRecords(records);		
 
		return result;
		
	}
	
}
